<template>
    <div>
        <el-alert
            title="记录游戏相关大事件, 如热更、强更等."
            type="warning"
            :closable="false"
            show-icon>
        </el-alert>
        <el-divider></el-divider>
        <div class="filter-container">
            <el-form label-position="left" :inline="true">
                <el-form-item label="更新时间">
                    <picker-datetime-range v-model="search.time"></picker-datetime-range>
                </el-form-item>
                <el-form-item label="">
                    <el-button class="filter-item" icon="el-icon-search" type="primary" :loading="loading" @click="pageNo = 1;handleSearch()">搜索</el-button>
                    <el-button class="filter-item" icon="el-icon-plus" type="primary" @click="create" plain>添加大事件</el-button>
                </el-form-item>
            </el-form>
        </div>

        <vxe-table
            :loading="loading"
            ref="xTable"
            stripe
            border
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%"
            class="text-center"
            @selection-change="handleSelectionChange">
            <vxe-table-column type="checkbox" width="40"/>
            <vxe-table-column title="事件时间" field="time" width="140"/>
            <vxe-table-column title="事件标题" field="title" width="150"/>
            <vxe-table-column title="事件内容" field="content"/>
            <vxe-table-column title="创建时间" field="created_at" width="160"/>
            <vxe-table-column title="更新时间" field="updated_at" width="160"/>
            <vxe-table-column title="操 作" width="150px">
                <template slot-scope="scope">
                    <el-button
                        type="primary"
                        @click="edit(scope.row)">
                        编辑
                    </el-button>
                    <destroy-confirm
                        route="appNews/Destroy"
                        :primary="scope.row.id"
                        @success="handleSearch">
                    </destroy-confirm>
                </template>
            </vxe-table-column>
        </vxe-table>

        <div class="mt-10 pull-left">
            <destroy-multiple
                route="appNews/DestroyMultiple"
                :selected="selected"
                @success="handleSearch">
                <el-button type="danger" plain>删除选中</el-button>
            </destroy-multiple>
        </div>

        <el-pagination
            class="pull-right clearfix"
            background
            :current-page.sync="pageNo"
            :page-sizes="pageOptions.pageSizesList"
            :page-size="pageSize"
            :layout="pageOptions.layout"
            :total="totalDataNumber"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange">
        </el-pagination>

        <el-dialog
            :title="form.data && form.data.id ? '编辑大事件' : '添加大事件'"
            :visible.sync="form.show"
            width="30%"
            :append-to-body="true"
            center>
            <dialog-form
                :data="form.data"
                v-on:submit="onFormSubmit"
                v-on:cancel="form.show = false">
            </dialog-form>
        </el-dialog>
    </div>
</template>

<script>
import dialogForm from './form'
export default {
    components: {
        dialogForm,
    },
    data() {
        return {
            search: {
                time: [],
            },
            loading: false,
            selected: [],
            tableData: [],
            pageNo: 1,
            pageSize: 20,
            totalDataNumber: 0,
            pageOptions: this.$store.state.backend.pageOptions,
            options: this.$store.state.appNews.options,
            form: {
                data: {},
                show: false,
            },
            dialogNotice: {
                show: false,
            }
        }
    },
    computed: {

    },
    methods: {
        handleSizeChange(val) {
            this.pageNo = 1;
            this.pageSize = parseInt(`${val}`);
            this.handleSearch();
        },
        handleCurrentChange(val) {
            this.pageNo = parseInt(`${val}`);
            this.handleSearch();
        },
        handleSelectionChange(val) {
            this.selected = val;
        },
        handleSearch() {
            this.loading = true;
            let filter = Object.assign({}, this.search, {
                page: this.pageNo,
                pageSize: this.pageSize,
            });
            this.$store.dispatch('appNews/Lists', filter).then(res => {
                this.tableData = res.data.data;
                this.totalDataNumber = res.data.meta.total;
            }).finally(() => {
                this.loading = false;
            });
        },
        initData() {
            this.handleSearch();
        },
        create() {
            this.form.data = {};
            this.form.show = true;
        },
        edit(row) {
            this.form.data = row;
            this.form.show = true;
        },
        onFormSubmit() {
            this.form.show = false;
            this.handleSearch();
        },
    },
    created() {
        this.initData();
    }
}
</script>

<style scoped>

</style>

